package com.itcast.googleplay.fragment;

import java.util.List;
import java.util.Random;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.itcast.googleplay.protocol.HotProtocol;
import com.itcast.googleplay.ui.FlowLayout;
import com.itcast.googleplay.ui.FlowLayout1;
import com.itcast.googleplay.ui.FlowLayout2;
import com.itcast.googleplay.ui.LoadingPage.ResultState;
import com.itcast.googleplay.utils.DrawableUtil;
import com.itcast.googleplay.utils.UIUtils;


public class HotFragment extends BaseFragment {

	private List<String> hotList;

	@Override
	public ResultState onSubLoad() {
		HotProtocol hotProtocol = new HotProtocol();
		hotList = hotProtocol.getData("hot", 0, "");
		if(hotList!=null){
			if(hotList.size()>0){
				return ResultState.STATE_SUCCESSED;
			}
			return ResultState.STATE_EMPTY;
		}
		return ResultState.STATE_ERROR;
	}

	@Override
	public View onCreateSubSuccessedView() {
		//1.使用FlowLayout实现流式布局效果
//		FlowLayout flowLayout = new FlowLayout(UIUtils.getContext());
//		FlowLayout1 flowLayout = new FlowLayout1(UIUtils.getContext());
		FlowLayout2 flowLayout = new FlowLayout2(UIUtils.getContext());
		
		int padding = UIUtils.dip2px(10);
		flowLayout.setPadding(padding, padding, padding, padding);
		//2.循环集合,将集合中的字符串维护成textView中的内容,添加到flowLayout中
		for (int i = 0; i < hotList.size(); i++) {
			TextView textView = new TextView(UIUtils.getContext());
			textView.setText(hotList.get(i));
			textView.setTextColor(Color.BLACK);
			//3.维护textView背景,颜色随机,圆角矩形(xml-->shape),采用java代码绘制圆角矩形
			
			int red = 30+new Random().nextInt(210);
			int green = 30+new Random().nextInt(210);
			int blue = 30+new Random().nextInt(210);
			
			int rgb = Color.rgb(red, green, blue);
			int radius = UIUtils.dip2px(10);
			
			
//			//在工具类中,指定颜色,和圆角半径绘制圆角矩形图片
			Drawable drawableBitmapNormal = DrawableUtil.drawableBitmap(rgb,radius);
			Drawable drawableBitmapPress = DrawableUtil.drawableBitmap(Color.GRAY,radius);
//			//将上一个步骤绘制的图片,作为textView背景展示
//			textView.setBackgroundDrawable(drawableBitmap);
			
			StateListDrawable stateListDrawable = DrawableUtil.getStateListDrawable
					(drawableBitmapNormal,drawableBitmapPress);
			textView.setBackgroundDrawable(stateListDrawable);
			
			//设置文字间距
			textView.setPadding(padding, padding,padding, padding);
			//居中
			textView.setGravity(Gravity.CENTER);
			//设置字体颜色
			textView.setTextColor(Color.WHITE);
			textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
			
			textView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					//点击事件,是在按下的时候触发,抬起的是触发
					Toast.makeText(UIUtils.getContext(),"选中了", 0).show();
				}
			});
			
			
			//最后循环过程中textView需要添加到flowLayout中
			flowLayout.addView(textView);
			
			
		}
		return flowLayout;
	}
}
